<?php

# I N C L U D E S
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/files.php";
include "../includes/text.php";
include "../includes/tableproperties.php";
include "../includes/shopping_cart.php";
include "../includes/init_front.php";

#Get id from url
$course_id = $_GET["id"];

#main output of html page
static $OUPUT_CONTENT ="";

$content_left_side ="";

$page_path = "";

#This value store the current page id
$current_id = "";

$course_number = str_replace('mres_','',$course_id);

//copy ("../assets/uploads/","../files/")

//echo $course_id;

//exec("tar -cvwf file.tar /usr/pkg/share/httpd/mrt.cmr.uq.edu.au/newmrt/mrt/files/mres_7001");

//IterationPages($course_id);

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-AU\" xml:lang=\"en-AU\">\n";
echo "<!-- Created by Cameron Morrow for Zone 4 -->\n";
echo "<head>\n";
echo "\t<title>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</title>\n";
echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
echo "\t<script language=\"javascript\" type=\"text/javascript\" src=\"../site_specific/assets/scripts/tct.js\"></script>\n";
echo "\t<style type=\"text/css\">\n";
echo "\t\t@import url(\"../site_specific/assets/styles/main.css\");\n";
echo "\t</style>\n";
echo "</head>\n";

echo "<body>";

# Start container
echo "<div id=\"container\">";

	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# H E A D E R
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	echo "<div id=\"header\">";
	echo "<h1>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</h1>\n";
	echo "</div>";

echo "<div id=\"nav\">\n";
echo "<hr/>";
echo "</div>";

echo "<div style=\"margin-bottom:auto;margin-right:auto;margin-left:50px;margin-top:50px;\">";

echo "<div id=\"zip_column\" style=\"overflow:scroll;height:400px;width:600px;border-style:solid;
border-width:1px;\">";

GenerateZip($course_id);

echo "</div>";

echo "<span style=\"color:red;font-size:xx-large;\">operation finished!!!</span><br/> <a href=\"../files/".$course_id.".zip\">Download ZIP</a>&nbsp;&nbsp;<a href=\"../site/".$course_id.".php\">Go Back</a>";

echo "</div>";

echo "</body>";

echo "</html>";


function GenerateZip($course_id){
	
	$zip_file = $course_id.".zip";
	
	$images = $course_id;
	
	//echo "files:".$images."<br/>";
	
	if ($handle = opendir($images))  
	{
		//echo "zip"."<br/>";
		$zip = new ZipArchive();
		
		if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) 
		{
			exit("cannot open <$filename>\n");
		}
		
		//echo "zip_file:".$zip_file;
		
		while (false !== ($file = readdir($handle))) 
		{   
			//echo $file."<br/>";
			if($handle1 = opendir($images.'/'.$file)){
				
				if($file != '.' && $file != '..' && $file != ''){
					
					while (false !== ($file1 = readdir($handle1))) {
						
						if($handle2 = opendir($images.'/'.$file.'/'.$file1)){
							
							if($file1 != '.' && $file1 != '..' && $file1 != ''){
								
								while (false !== ($file2 = readdir($handle2))){
									
									if($file2 != '.' && $file2 != '..' && $file2 != ''){
											
										if($zip->addFile($images.'/'.$file.'/'.$file1.'/'.$file2)){
											
											echo date("H:i:s")."------".$images.'/'.$file.'/'.$file1.'/'.$file2."has been added successfully<br/><br/>";
										}
										else{
											
											echo date("H:i:s")."------".$images.'/'.$file.'/'.$file1.'/'.$file2."The transfer for $file1 has failure<br/><br/>";
										}
										
										echo "<script type=\"text/javascript\">var objdiv= document.getElementById('zip_column');objdiv.scrollTop = objdiv.scrollHeight;</script>";
									}
								}
							}
						}
						else{
							if($file1 != '.' && $file1 != '..' && $file1 != ''){
								
								if($zip->addFile($images.'/'.$file.'/'.$file1)){
												
									echo date("H:i:s")."------".$images.'/'.$file.'/'.$file1."has been added successfully<br/><br/>";
								}
								else{
												
									echo date("H:i:s")."------".$images.'/'.$file.'/'.$file1."The transfer for $file1 has failure<br/><br/>";
								}
								
								echo "<script type=\"text/javascript\">var objdiv= document.getElementById('zip_column');objdiv.scrollTop = objdiv.scrollHeight;</script>";
							}
						}
					}
				}
			}
			else{
				if($file != '.' && $file != '..' && $file != ''){
					
					if($zip->addFile($images.'/'.$file)){
												
						echo date("H:i:s")."------".$images.'/'.$file."has been added successfully<br/><br/>";
					}
					else{
									
						echo date("H:i:s")."------".$images.'/'.$file."The transfer for $file1 has failure<br/><br/>";
					}
					
					echo "<script type=\"text/javascript\">var objdiv= document.getElementById('zip_column');objdiv.scrollTop = objdiv.scrollHeight;</script>";
				}
			}
			//if($file != '.' && $file != '..' && $file != ''){
//				
//				if($handle = opendir($images.'/'.$file)){
//					
//					while (false !== ($file1 = readdir($handle))) 
//					{
//						if($handle1 = opendir($images.'/'.$file.'/'.$file1)){
//								
//							if($file1 != '.' && $file1 != '..' && $file1 != ''){
//								
//								while (false !== ($file2 = readdir($handle1))) {
//								
//									if($handle2 = opendir($images.'/'.$file.'/'.$file1.'/'.$file2)){ 
//											
//										if($file2 != '.' && $file2 != '..' && $file2 != ''){
//												
//											if($zip->addFile($images.'/'.$file.'/'.$file1.'/'.$file2)){//tmp/1/Map-Qing_Dynasty_1911.jpg
//												echo $images.'/'.$file."has been added successfully<br/>";
//											}
//											else{
//												echo $images.'/'.$file."The transfer for $file1 has failure<br/>";
//											}
//										}
//									}
//									else{
//										if($zip->addFile($images.'/'.$file.'/'.$file1.'/'.$file2)){//tmp/1/Map-Qing_Dynasty_1911.jpg
//											echo $images.'/'.$file."has been added successfully<br/>";
//										}
//										else{
//											echo $images.'/'.$file."The transfer for $file1 has failure<br/>";
//										}
//									}		   
//								}  
//							}
//							else{
//								if($zip->addFile($images.'/'.$file.'/'.$file1)){//tmp/1/Map-Qing_Dynasty_1911.jpg
//
//									echo $images.'/'.$file."has been added successfully<br/>";
//								}
//								else{
//								
//									echo $images.'/'.$file."The transfer for $file1 has failure<br/>";
//								}	  
//							}
//						}
//					}
//				}
//				else{
//					if($zip->addFile($images.'/'.$file)){//tmp/1/Map-Qing_Dynasty_1911.jpg
//							  
//						echo $images.'/'.$file."has been added successfully<br/>";
//					}
//					else{
//						  
//						echo $images.'/'.$file."The transfer for $file1 has failure<br/>";
//					}	
//				}
//			}
		   
		}
		closedir($handle);
		$zip->close();
	}
}

function IterationPages($id = ''){
	
	global $PAGES;
	
	global $USER;
	
	global $current_id;
	
	$top_level_pages = $PAGES -> getChildrenOfPage($id);
	
	//print_r($top_level_pages);
	
	generateHTMLFile($id);

	if(count($top_level_pages)>0){
	
		# For each
		foreach ($top_level_pages -> getPageIDs() as $nav_page_id) {
		
			# Get page details
			$nav_page = $top_level_pages -> getPage($nav_page_id);
		
			# Get list of groups authorized to view this page
			$nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
			
			# Get List of groups of users
			$nav_group_authorized= explode(",", $USER -> getGroup());
		
			# Ensure it can be viewed at current authorization
			$nav_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($nav_page_id));
		
			# Ensure it's in the navigation
			//$nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");
		
			# Get class for <li> item
			$item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();
		
			if ($nav_page_authorized) {
				
				$current_id = $nav_page_id;
				
				IterationPages($nav_page_id);		
			}
		}
	}		
}

function generateHTMLFile($id){
	
	global $PROJECT_TITLE;
	global $PAGE_NAME;
	global $course_id;
	global $PAGE_ID;
	global $PAGES;
	global $content_left_side;
	global $page_path;
	global $current_id;
	global $PROJECT_DB_TABLES;

	$PRINT_OUTPUT = "";
	
	$PRINT_OUTPUT .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
	$PRINT_OUTPUT .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-AU\" xml:lang=\"en-AU\">\n";
	$PRINT_OUTPUT .= "<!-- Created by Cameron Morrow for Zone 4 -->\n";
	$PRINT_OUTPUT .= "<head>\n";
	$PRINT_OUTPUT .= "\t<title>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</title>\n";
	$PRINT_OUTPUT .= "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
	$PRINT_OUTPUT .= "\t<script language=\"javascript\" type=\"text/javascript\" src=\"../js/tct.js\"></script>\n";
	$PRINT_OUTPUT .= "\t<style type=\"text/css\">\n";
	$PRINT_OUTPUT .= "\t\t@import url(\"../css/main.css\");\n";
	$PRINT_OUTPUT .= "\t</style>\n";
	$PRINT_OUTPUT .= "</head>\n";

	$PRINT_OUTPUT .= "<body onload=\"parseImagePopups();\">\n";

	# Start container
	$PRINT_OUTPUT .= "<div id=\"container\">";

	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# H E A D E R
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	$PRINT_OUTPUT .= "<div id=\"header\">";
	$PRINT_OUTPUT .= "<h1>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</h1>\n";
	$PRINT_OUTPUT .= "</div>";
	
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# M A I N   C O N T E N T
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
	$PRINT_OUTPUT .= "<div id=\"maincontent\">\n";
	$PRINT_OUTPUT .= "<div id=\"maincontentleft\">\n";
	
	
	$flag_list_end = false; //this flag is used to judge whether the list will be ended or not.
	
	//$content_left_side = '';
	
	$page_path = $PAGES -> getPathToPage($id);
	
	$PRINT_OUTPUT .= "<br/>";
	
	$course_pages = $PAGES -> getChildrenOfPage($course_id);
	
	$content_left_side = "";
	
	IterationList($course_pages,$course_id);
	
	$PRINT_OUTPUT .= $content_left_side;
	
	//echo $content_left_side."<br/>";
	  
	$PRINT_OUTPUT .= "</div>";
	
	# RIGHT
	$PRINT_OUTPUT .= "<div id=\"maincontentright\" style=\"font-family:Arial;\"><div class=\"cw\">";
	  
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    # P A G E   C O N T E N T
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	  
	if (!isset($main_output)) {
		
		$PAGE = getPageDetails($id);
		
		$PAGE_CONTENT = $PAGE -> getData("p_content");
		# Prep content
		$main_output = parseFlashLinks(parseFlashXML($PAGE_CONTENT));
		
		#Change the original hyper link style to the one with css
		$main_output = ChangeHyperLinkStyle($main_output);
		
		$multiple_choice_searching_item_begin="<multiple-choice>";
		
		$multiple_choice_searching_item_end="</multiple-choice>";
		
		if(strpos($main_output,$multiple_choice_searching_item_begin)&& strpos($main_output,$multiple_choice_searching_item_begin)){
			
			$main_output=ReplaceMulitpleChoice($main_output,false);
		}
		
		$seaching_content = getRecord("SELECT gl_letter,gl_term,gl_descripto FROM " . $PROJECT_DB_TABLES["glossary"] . " order by gl_letter");
		
		//echo "SELECT gl_letter,gl_term,gl_descripto FROM " . $PROJECT_DB_TABLES["glossary"] . " order by gl_letter"."<br/>";
		
		$question_searching_item = "<a href=\"www.uq.edu.au\">solution</a>";
		
		$main_output = str_replace($question_searching_item,"",$main_output);
		
		$main_output = ReplaceFontFamily($main_output);
		
		$main_output = marked_keyword_replace($main_output,$seaching_content);
		
		//print_r($seaching_content);
					
		$main_output = glossary_replace($main_output,$seaching_content);						  
		
		#remove the none link Indentifier
		$main_output = RemoveHyperlink($main_output,"***","###");
		
		#replace the reference indetifier.
		$main_output = Replace_reference($main_output);	
		
		$main_output = Replace_URL($main_output);
													
		//if(strpos($main_output,$question_searching_item)){
//			
//			$main_output = removetable($main_output);
//			
//			$exersice_array = explode($question_searching_item,$main_output);
//			
//			$main_output = $exersice_array[0];
//			
//			$main_output .= "<a href=\"javascript:expandAnswer('".$PAGE -> getData("p_id")."',1,'div_quesiton_answer_0','answer_expand_img_0');\"><img id=\"answer_expand_img_0\" border=\"0\" src=\"../assets/images/tree/folder_closed_1.png\"></a><div id=\"div_quesiton_answer_0\"></div>";
//		}
//		
//		$main_output = str_replace($question_searching_item,"",$main_output);	
	}
	  
	# MAIN
	$PRINT_OUTPUT .= $main_output;
	  
	$PRINT_OUTPUT .= "</div>";
	
	$PRINT_OUTPUT .= "</div>";
	
	$PRINT_OUTPUT .= "<div id=\"popup\" >";

	$PRINT_OUTPUT .= "<div id=\"handle\" style=\"\">";
 
    $PRINT_OUTPUT .= "<form name=\"popupform\" id=\"popupform\" action=\"" . $id . ".php\" method=\"get\">";
 
 	$PRINT_OUTPUT .= "</form>";
 
 	$PRINT_OUTPUT .= "</div>";
	
	$PRINT_OUTPUT .= "</div>";
	
	$PRINT_OUTPUT .= "</body>";
	
	$PRINT_OUTPUT .= "</html>";
	
	deleteFile("../files/".$course_id."/html/",$id.".html");
	
	//echo $PRINT_OUTPUT;
	if(createFile("../files/".$course_id."/html/",$id.".html",$PRINT_OUTPUT)){
	
		echo "$id has been created"."<br/>";		 
	}
	else{
		
		echo "$id creation request has been rejected"."<br/>";
	}
}

#Iteration function to generate left side menu	
function IterationList($pages,$page_id){

	global $content_left_side;
	global $USER;
	global $PAGES;
	global $page_path;
	global $current_id;
	
	$PAGE_ID = $current_id;
	
	$content_left_side .= "<ul>";
	
	if(count($pages)>0) {
		
		foreach($pages -> getPageIDs() as $lsm_page_id){
			
			$lsm_page = $pages -> getPage($lsm_page_id);
			
			$lsm_page_authorized = explode(",", $lsm_page -> getData("p_access"));
			
			$lsm_group_authorized = explode(",", $USER -> getGroup());
			
			# Ensure it can be viewed at current authorization
			$lsm_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($lsm_page_id));
			
			$lsm_page_in_navigation = ($lsm_page -> getData("p_innavigation") == "1");

			if ($lsm_page_authorized && $lsm_page_in_navigation) {
				
//				echo $lsm_page_id."___".$PAGE_ID."<br/>";
//				
//				print_r($page_path);
//				
//				echo "<br/>";
				
				if(in_array($lsm_page_id,$page_path)||$lsm_page_id==$PAGE_ID){
					
					if($lsm_page_id==$PAGE_ID){
						
						$content_left_side .= "<li><a href=\"".$lsm_page -> getData("p_id").".html\" style=\"font-family:Georgia,serif;color:#DF0101;\">" . $lsm_page -> getData("p_name") . "</a></li>";
					}
					else{
						
						$content_left_side .= "<li><a href=\"".$lsm_page -> getData("p_id").".html\" style=\"font-family:Georgia,serif;color:#4B088A;text-decoration: none;\">" . $lsm_page -> getData("p_name") . "</a></li>";
					}
					$next_level_pages = $PAGES -> getChildrenOfPage($lsm_page_id);
					
					echo "<br/>";
					
					IterationList($next_level_pages,$lsm_page_id);
				}
				else if($lsm_page_id!=$PAGE_ID&&!in_array($lsm_page_id,$page_path)){
					
					$content_left_side .= "<li><a href=\"" .$lsm_page -> getData("p_id").".html\" style=\"font-family:Georgia,serif;color:#4B088A;text-decoration: none;\">" . $lsm_page -> getData("p_name") . "</a></li>";
				}
			}
		}
		
	}
	
	$content_left_side .= "</ul>";
	
}
?>